package org.sixplusone.redblue;

import java.util.Arrays;
import java.util.Random;

public class Utils {
	public static int[] initArray(int len) {
		int[] result = new int[len];
		for (int i = 0; i < result.length; i++) {
			result[i] = i + 1;
		}
		return result;
	}

	public static int[] select(int[] arrayin, int n, Random random) {
		int[] array = arrayin.clone();
		assert array.length > n;
		int[] result = new int[n];

		shuffle(array, n, random);
		for (int i = 0; i < n; i++) {
			result[i] = array[array.length - i - 1];
		}

		Arrays.sort(result);
		return result;
	}

	public static void shuffle(int[] in, Random random) {
		int lastN = in.length;
		shuffle(in, lastN, random);
	}

	public static void shuffle(int[] in, int lastN, Random random) {
		assert lastN <= in.length;
		int range = in.length;
		for (int i = 0; i < lastN; i++) {
			int selected = random.nextInt(range);
			int copy = in[selected];
			in[selected] = in[range - 1];
			in[range - 1] = copy;
			range--;
		}
	}

}
